package com.java.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import com.java.utils.UserSession;

/**
* @ClassName: BaseJFrame
* @Description: 自定义一些共用操作
*
 */
public class BaseJFrame extends JFrame {
	private static final long serialVersionUID = 5602570535905658078L;

	public JFrame bJFrame;

	public UserSession session = UserSession.getInstance();

	public void initMenu(JFrame frame) {
		this.bJFrame = frame;
		/**
		 * 创建整个系统的菜单栏和菜单项
		 */
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		/**
		 * 创建菜单
		 */
		JMenu adminMenu = new JMenu("To Do Management");
		JMenuItem addDoctorItem = new JMenuItem("Add to-do list");
		JMenuItem doctorItem = new JMenuItem("To Do List");
		JMenuItem drawItem = new JMenuItem("To Do Calendar");
		adminMenu.add(addDoctorItem);
		adminMenu.add(doctorItem);
		adminMenu.add(drawItem);
		menuBar.add(adminMenu);

		addDoctorItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setEnabled(false);
				new AddTodoWindow(bJFrame, null);
			}
		});

		doctorItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setVisible(false);
				new AdminMainWindow();
			}
		});

		drawItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setEnabled(false);
				new TodoDrawWindow(bJFrame);
			}
		});

		JMenu commonMenu = new JMenu("Account Center");
		JMenuItem mine = new JMenuItem("My information");
		JMenuItem adminPswItem = new JMenuItem("Password modification");
		JMenuItem logout = new JMenuItem("Exit login");
		JMenuItem logoutexit = new JMenuItem("Exit the system");
		JMenuItem aboutSys = new JMenuItem("About");
		logout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				logoutSys();
			}
		});
		logoutexit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				exitSys();
			}
		});
		aboutSys.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ok("Welcome to use");
			}
		});
		mine.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ok("Current username：" + session.getUserName());
			}
		});

		adminPswItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bJFrame.setEnabled(false);
				new AdminPasswordWindow(bJFrame);
			}
		});
		commonMenu.add(mine);
		commonMenu.add(adminPswItem);
		commonMenu.add(logout);
		commonMenu.add(logoutexit);
		commonMenu.add(aboutSys);
		menuBar.add(commonMenu);

	}

	public void logoutSys() {
		if (confirm("Do you want to log out of the system and log in")) {
			session.logout();
			bJFrame.dispose();
			new LoginWindow();
		}
	}

	public void exitSys() {
		if (confirm("Whether to shut down the system")) {
			System.exit(0);
		}
	}

	public void ok(String msg) {
		JOptionPane.showMessageDialog(bJFrame, msg, "Prompt", JOptionPane.INFORMATION_MESSAGE);
	}

	public void error(String msg) {
		JOptionPane.showMessageDialog(bJFrame, msg, "Prompt", JOptionPane.ERROR_MESSAGE);
	}

	public void warning(String msg) {
		JOptionPane.showMessageDialog(bJFrame, msg, "Prompt", JOptionPane.WARNING_MESSAGE);
	}

	public void loading(String msg) {
		JOptionPane.showMessageDialog(bJFrame, msg, "Loading", JOptionPane.PLAIN_MESSAGE);
	}

	public boolean confirm(String msg) {
		int optionType = JOptionPane.YES_NO_OPTION; // 确认框的选项类型
		int messageType = JOptionPane.QUESTION_MESSAGE; // 消息类型（警告、信息、询问等）
		int result = JOptionPane.showConfirmDialog(null, msg, "Confirmation prompt", optionType, messageType); // 显示确认框并返回结果
		return result == JOptionPane.YES_OPTION;
	}

}
